package com.yty.Exception;

import com.yty.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result GlobalException(Exception e) {
        e.printStackTrace();
        return Result.error("服务器异常");
    }
    @ExceptionHandler
    public Result HanderDuplicateKeyException(DuplicateKeyException e) {
        e.printStackTrace();
        String em = e.getMessage().substring(e.getMessage().indexOf("Duplicate entry"));
        String[] s = em.split(" ");
        return Result.error(s[2]+" 与其他用户重复");
    }
}
